package com.suxin.netty.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author Tang
 * @classname ProtocolByteToMessageDecoder
 * @description [ 解码器 ]
 * @date 2021/7/18 11:29
 */
public class ProtocolByteToMessageDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        ProtocolMsg protocolMsg = new ProtocolMsg(){{
            int readLen = in.readInt();
            setLen(readLen);
            byte[] bt = new byte[readLen];
            in.readBytes(bt);
            setData(bt);
        }};
        out.add(protocolMsg);
    }
}